package com.cskaoyan.controller;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.domain.MarketUser;
import com.cskaoyan.service.CouponService;
import com.cskaoyan.service.CouponServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/wx/coupon")
public class WxCouponController {
    @Autowired
    CouponService couponService;
    @GetMapping("list")
    public BaseRespVo list(Integer page, Integer limit) {
        BaseRespVo baseRespVo=couponService.getList(page,limit);
        return baseRespVo;
    }
    @GetMapping("mylist")
    public BaseRespVo mylist(Integer page, Integer limit,Integer status) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId=0;
        if (subject.isAuthenticated()) {
            MarketUser principal = (MarketUser) subject.getPrincipal();
            userId = principal.getId();
        }
        BaseRespVo baseRespVo=couponService.getMylist(page,limit,status,userId);
        return baseRespVo;
    }
    @GetMapping("selectlist")
    public BaseRespVo selectList(Integer cartId,Integer grouponRulesId) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId=0;
        if (subject.isAuthenticated()) {
            MarketUser principal = (MarketUser) subject.getPrincipal();
            userId = principal.getId();
        }
        BaseRespVo baseRespVo=couponService.selectCoupon(cartId,grouponRulesId,userId);
        return baseRespVo;
    }
    @PostMapping("receive")
    public BaseRespVo receive(@RequestBody Map map) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId=0;
        if (subject.isAuthenticated()) {
            MarketUser principal = (MarketUser) subject.getPrincipal();
            userId = principal.getId();
        }
        BaseRespVo baseRespVo=couponService.receiveCoupon(map,userId);
        return baseRespVo;
    }

    @PostMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map map) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId=0;
        if (subject.isAuthenticated()) {
            MarketUser principal = (MarketUser) subject.getPrincipal();
            userId = principal.getId();
        }
        BaseRespVo baseRespVo=couponService.exchangeCoupon(map,userId);
        return baseRespVo;
    }

}
